using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class A_Sec_Scan_Login : Form
    {
        public A_Sec_Scan_Login()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        public int oldX, oldY;//移动窗体方法
        public string code;//验证码
        public string cpuInfo;//获取机器码
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text)||string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("账号或密码不能为空！");
                return;
            }
            else
            {
                if (textBox4.Text.Equals(code))
                {
                    //一致的情况
                }
                else
                {
                    MessageBox.Show("验证码错误！请重试！");
                    //刷新验证码
                    ReferCode();
                }
            }
        }

        private void pictureBox11_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void Login_Load(object sender, EventArgs e)
        {
            //加载验证码
            code = Sec_Config.Sec_CodeInfo.GenerateCheckCode();
            Bitmap image = Sec_Config.Sec_CodeInfo.CreateCheckCodeImage(code, 64, 30);//长高
            pictureBox1.Image = image;
            //加载机器码
            CpuInfo();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //刷新验证码
            ReferCode();
        }
        /// <summary>
        /// 刷新验证码公共方法
        /// </summary>
        public void ReferCode()
        {
            code = Sec_Config.Sec_CodeInfo.GenerateCheckCode();
            Bitmap image = Sec_Config.Sec_CodeInfo.CreateCheckCodeImage(code, 64, 30);//长高
            pictureBox1.Image = image;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //手动获取机器码的方法
            CpuInfo();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }
        /// <summary>
        /// 获取机器码
        /// </summary>
        public void CpuInfo()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementBaseObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    Console.WriteLine("系统机器码：" + "\r\n" + cpuInfo);
                    textBox1.Text = cpuInfo;
                    break;
                }
                moc = null;
                mc = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误提示：" + "无法获取到机器码，请重试！" + "\r\n" + ex.ToString());
                return;
            }
        }
    }
}
